﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraReports.UI;
namespace Exact_VAS
{
    public partial class DebtorStatementForm : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
        public DebtorStatementForm(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            this.LoadDataInitialize();
        }

        private void LoadDataInitialize()
        {
            var queryST = from ST in DS.ISHCMC_V_StudentListWithDebtors
                          select ST;
            this.iSHCMCVStudentListWithDebtorBindingSource.DataSource = queryST;
            this.dateEdit_From.EditValue = DateTime.Now.Date;
            this.dateEdit_To.EditValue = DateTime.Now.Date;
           
        }
        
        private void btnPrint_Click(object sender, EventArgs e)
        {
            DateTime FromDate = Convert.ToDateTime(this.dateEdit_From.EditValue, culture);
            DateTime ToDate = Convert.ToDateTime(this.dateEdit_To.EditValue, culture);
            XtraReport RootRpt = new XtraReport();
            
            if (this.gridView_Student.GetSelectedRows().Length > 0)
            {
                foreach (int i in this.gridView_Student.GetSelectedRows())
                {
                    ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(i);
                    double OBPaidAmount = 0;
                    double OBInvoiceAmount = 0;
                    double TotalPaidAmount = 0;
                    double TotalInvoiceAmount = 0;
                    double CBInvoiceAmount = 0;
                    double OverdueAmount = 0;

                    if (this.cbxGroupby.SelectedItem.ToString() == "Student")
                    {
                        var queryOB = from OBSTA in DS.ISHCMC_V_StatementAccounts
                                      where OBSTA.StudentID == itemStudent.StudentID
                                      && OBSTA.PostingDate < FromDate
                                      select OBSTA;


                        foreach (ISHCMC_V_StatementAccount itemSTA in queryOB)
                        {
                            if (itemSTA.PaidAmount.HasValue)
                            {
                                OBPaidAmount += itemSTA.PaidAmount.Value;
                            }
                            if (itemSTA.InvoiceAmount.HasValue)
                            {
                                OBInvoiceAmount += itemSTA.InvoiceAmount.Value;
                            }

                        }


                        DebtorStatementRpt rpt = new DebtorStatementRpt();
                        var querySTA = from STA in DS.ISHCMC_V_StatementAccounts
                                       where STA.StudentID == itemStudent.StudentID
                                       && STA.PostingDate >= FromDate && STA.PostingDate <= ToDate
                                       select STA;
                        List<ISHCMC_V_StatementAccount> ListSTA = new List<ISHCMC_V_StatementAccount>();

                        foreach (ISHCMC_V_StatementAccount itemSTA in querySTA)
                        {
                            itemSTA.OBPaidAmount = OBPaidAmount;
                            itemSTA.OBInvoiceAmount = OBInvoiceAmount;
                            if (itemSTA.PaidAmount.HasValue)
                            {
                                TotalPaidAmount += itemSTA.PaidAmount.Value;
                            }
                            if (itemSTA.InvoiceAmount.HasValue)
                            {
                                TotalInvoiceAmount += itemSTA.InvoiceAmount.Value;
                            }
                            if (itemSTA.DueDate < DateTime.Now.Date)
                            {
                                if (itemSTA.InvoiceAmount.HasValue)
                                {
                                    OverdueAmount += itemSTA.InvoiceAmount.Value;
                                }
                            }

                        }
                        CBInvoiceAmount = (OBInvoiceAmount + TotalInvoiceAmount) - (OBPaidAmount + TotalPaidAmount);

                        foreach (ISHCMC_V_StatementAccount itemSTA in querySTA)
                        {
                            itemSTA.OBPaidAmount = OBPaidAmount;
                            itemSTA.OBInvoiceAmount = OBInvoiceAmount;

                            itemSTA.CBInvoiceAmount = CBInvoiceAmount;
                            itemSTA.FromDate = FromDate;
                            itemSTA.ToDate = ToDate;
                            itemSTA.OverdueAmount = OverdueAmount;
                            ListSTA.Add(itemSTA);
                        }

                        rpt.LoadDataSource(ListSTA);
                        rpt.CreateDocument();
                        RootRpt.Pages.AddRange(rpt.Pages);
                     
                    }
                    else
                    {

                        var queryOB = from OBSTA in DS.ISHCMC_V_StatementAccounts
                                      where OBSTA.InvoiceDebtor == itemStudent.InvoiceDebtor
                                      && OBSTA.PostingDate < FromDate
                                      select OBSTA;

                        
                        foreach (ISHCMC_V_StatementAccount itemSTA in queryOB)
                        {
                            if (itemSTA.PaidAmount.HasValue)
                            {
                                OBPaidAmount += itemSTA.PaidAmount.Value;
                            }
                            if (itemSTA.InvoiceAmount.HasValue)
                            {
                                OBInvoiceAmount += itemSTA.InvoiceAmount.Value;
                            }

                        }


                        ParentDebtorStatementRpt rpt = new ParentDebtorStatementRpt();
                        var querySTA = from STA in DS.ISHCMC_V_StatementAccounts
                                       where STA.InvoiceDebtor == itemStudent.InvoiceDebtor
                                       && STA.PostingDate >= FromDate && STA.PostingDate <= ToDate
                                       select STA;

                        var querySTAH = from STAH in DS.ISHCMC_V_StatementAccounts
                                       where STAH.InvoiceDebtor == itemStudent.InvoiceDebtor
                                       && STAH.PostingDate >= FromDate && STAH.PostingDate <= ToDate
                                       select STAH;
                        List<ISHCMC_V_StatementAccount> ListSTA = new List<ISHCMC_V_StatementAccount>();
                        List<ISHCMC_V_StatementAccount> ListHeader = new List<ISHCMC_V_StatementAccount>();
                        if (querySTA.Count() > 0 && querySTAH.Count() > 0)
                        {
                            foreach (ISHCMC_V_StatementAccount itemSTA in querySTA)
                            {
                                itemSTA.OBPaidAmount = OBPaidAmount;
                                itemSTA.OBInvoiceAmount = OBInvoiceAmount;
                                if (itemSTA.PaidAmount.HasValue)
                                {
                                    TotalPaidAmount += itemSTA.PaidAmount.Value;
                                }
                                if (itemSTA.InvoiceAmount.HasValue)
                                {
                                    TotalInvoiceAmount += itemSTA.InvoiceAmount.Value;
                                }
                                if (itemSTA.DueDate < DateTime.Now.Date)
                                {
                                    if (itemSTA.InvoiceAmount.HasValue)
                                    {
                                        OverdueAmount += itemSTA.InvoiceAmount.Value;
                                    }
                                }

                            }
                            CBInvoiceAmount = (OBInvoiceAmount + TotalInvoiceAmount) - (OBPaidAmount + TotalPaidAmount);

                            foreach (ISHCMC_V_StatementAccount itemSTA in querySTA)
                            {
                                itemSTA.OBPaidAmount = OBPaidAmount;
                                itemSTA.OBInvoiceAmount = OBInvoiceAmount;

                                itemSTA.CBInvoiceAmount = CBInvoiceAmount;
                                itemSTA.FromDate = FromDate;
                                itemSTA.ToDate = ToDate;
                                itemSTA.OverdueAmount = OverdueAmount;
                                ListSTA.Add(itemSTA);
                            }

                            ISHCMC_V_StatementAccount itemHeader = querySTAH.First();
                            itemHeader.OBInvoiceAmount = OBInvoiceAmount;
                            itemHeader.OBPaidAmount = OBPaidAmount;
                            itemHeader.OverdueAmount = OverdueAmount;
                            itemHeader.CBInvoiceAmount = CBInvoiceAmount;
                            itemHeader.FromDate = FromDate;
                            itemHeader.ToDate = ToDate;
                            itemHeader.TotalPaidAmount = TotalPaidAmount;
                            itemHeader.TotalInvoiceAmount = TotalInvoiceAmount;
                            ListHeader.Add(itemHeader);
                            rpt.LoadDataSource(ListHeader, ListSTA);
                            rpt.CreateDocument();
                            RootRpt.Pages.AddRange(rpt.Pages);
                        }
                        else
                        {
                            MessageBox.Show("No data in this period");
                        }
                    }
                }
                ReportPrintTool ReportPrint = new ReportPrintTool(RootRpt);
                ReportPrint.ShowPreview();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            var queryST = from ST in DS.ISHCMC_V_StudentListWithDebtors
                          select ST;
            this.iSHCMCVStudentListWithDebtorBindingSource.DataSource = queryST;
        }
    }
}